class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int left = 0;//左边界
        int right = n-1;//右边界
        int top = 0;//上边界
        int index = n-1;//下边界
        vector<vector<int>> ret(n,vector<int>(n));
        int tmp = 1;
        while(left<=right&&top<=index)
        {
            //填写上边界的，从左到右
            for(int i = left;i<=right;i++)
            {
                ret[top][i] = tmp++;
            }
            //填写完毕之后上边界++
            top++;
            //填写右边界，从上到下
            for(int i = top;i<=index;i++)
            {
                ret[i][right] = tmp++;
            }
            //填写完毕之后让右边界--
            right--;
            //填写下边界，从右到左
            for(int i = right;i>=left;i--)
            {
                ret[index][i] = tmp++;
            }
            //填写完毕之后让下边界--
            index--;
            //最后填写左边界,从下到上
            for(int i = index;i>=top;i--)
            {
                ret[i][left] = tmp++;
            }
            left++;
        }
        return ret;
    }
};